package com.cskaoyan.service;

import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.param.AdParam;
import com.cskaoyan.bean.pojo.MallAd;
import com.cskaoyan.bean.pojo.MallAdExample;
import com.cskaoyan.mapper.MallAdMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Transactional(rollbackFor = Exception.class)
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MallAdMapper mallAdMapper;

    @Override
    public BaseData<MallAd> queryList(AdParam param) {
        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(), param.getLimit());
        MallAdExample mallAdExample = new MallAdExample();
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();
        if (param.getName() != null) {
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if (param.getContent() != null) {
            criteria.andContentLike("%" + param.getContent() + "%");
        }

        mallAdExample.setOrderByClause(param.getSort() + " " + param.getOrder());

//        显示的都是未删除的
        criteria.andDeletedEqualTo(false);
        mallAdExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallAd> mallAds = mallAdMapper.selectByExample(mallAdExample);
        long total = mallAdMapper.countByExample(mallAdExample);
        return BaseData.ok(mallAds, total);
    }

    @Override
    public int deleteAd(MallAd mallAd) {
        MallAdExample mallAdExample = new MallAdExample();
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();
        criteria.andIdEqualTo(mallAd.getId());

        MallAd mallAd1 = new MallAd();
        mallAd1.setDeleted(true);


        int i = mallAdMapper.updateByExampleSelective(mallAd1, mallAdExample);
        if (i > 0) {

            int affectedRows = mallAdMapper.updateByExampleSelective(mallAd1, mallAdExample);
            if (affectedRows > 0) {

                return 200;
            }
        }
        return 404;
    }


    @Override
    public int updateAd(MallAd mallAd) {
        MallAdExample mallAdExample = new MallAdExample();
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();
        criteria.andIdEqualTo(mallAd.getId());

        int i = mallAdMapper.updateByExampleSelective(mallAd, mallAdExample);
        if (i > 0) {

            int affectedRows = mallAdMapper.updateByExampleSelective(mallAd, mallAdExample);
            if (affectedRows > 0) {

                return 200;
            }
            return 404;
        }
        return 404;
    }

    @Override
    public MallAd createAd(MallAd mallAd) {
        int affectedRows = mallAdMapper.insertSelective(mallAd);
        if (affectedRows > 0) {
            Date date = new Date();
            mallAd.setAddTime(date);
            mallAd.setUpdateTime(date);
            return mallAd;
        }
        return null;
    }
}

